﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TVNNewDBLayer.DAL
{
    class CategoryDAL:ICommon<TB_Category, int>
    {

        TVNNewsDataContext db;

        public CategoryDAL()
        {
            db = new TVNNewsDataContext();
        }

        public CategoryDAL(TVNNewsDataContext db)
        {
            this.db = db;
        }

        public List<TB_Category> GetAll()
        {
            return db.TB_Categories.ToList();
        }

        public TB_Category Get(int p)
        {
            return db.TB_Categories.SingleOrDefault(a=>a.CategoryID == p);
        }

        public TB_Category Insert(TB_Category p)
        {
            try
            {
                db.TB_Categories.InsertOnSubmit(p);
                db.SubmitChanges();
                return p;
            }
            catch (Exception)
            {
                throw;
            }
        }

        public bool Update(TB_Category p)
        {
            try
            {
                db.SubmitChanges();
                return true;
            }
            catch (Exception)
            {
                return true;
                throw;
            }
        }

        public bool Delete(int p)
        {
            try
            {
                TB_Category c = Get(p);
                db.TB_Categories.DeleteOnSubmit(c);
                db.SubmitChanges();
                return true;
            }
            catch (Exception)
            {
                return true;
            }
        }
    }
}
